/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.dao.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import org.jdom.Element;

import who.tmap.bo.BizObject;
import who.tmap.bo.BizObjects;
import who.tmap.bo.MultiLayerThematicMap;
import who.tmap.bo.ThematicMap;
import who.tmap.bo.ThematicMapLayer;
import who.tmap.bo.impl.BizObjectFactory;
import who.tmap.dao.MultiLayerThematicMapDAO;
import who.tmap.dao.TMapDAOException;
import who.tmap.dao.ThematicMapDAO;
import who.tmap.dao.cd.impl.CDDAOFactory;

public class MultiLayerThematicMapDAOImpl extends BaseDAOImpl implements MultiLayerThematicMapDAO {

    private static final String VERSION ="1.1";	
    
    private static final DAOFactory daof =DAOFactory.getInstance();
    private static final CDDAOFactory cddaof =CDDAOFactory.getInstance();
	
    @SuppressWarnings("unchecked")
	public BizObject loadXml( Element elt ) throws TMapDAOException {
        MultiLayerThematicMap mltm =null;

        try {
        	mltm =(MultiLayerThematicMap)bof.get( BizObjectFactory.MULTI_LAYER_THEMATIC_MAP );
        	mltm.setLayers( new ArrayList<ThematicMapLayer>() );
        	        	
	        Iterator it =elt.getChildren("Layer").iterator();
	        while( it.hasNext() ) {
	            Element layerElt =(Element)it.next();
	            
	            String name =layerElt.getAttributeValue( "name" );
	            String file =layerElt.getAttributeValue( "file" );
	            Boolean visible =Boolean.valueOf( layerElt.getAttributeValue( "visible" ) );
	            Boolean baseMapVisible =Boolean.valueOf( layerElt.getAttributeValue( "baseMapVisible" ) );
	            Boolean cdVisible =Boolean.valueOf( layerElt.getAttributeValue( "cartographicDisplayVisible" ) );
	            Boolean annotationsVisible =Boolean.valueOf( layerElt.getAttributeValue( "annotationsVisible" ) );		            
	            
	        	ThematicMapDAO thematicMapDao =(ThematicMapDAO)daof.get( BizObjects.THEMATIC_MAP );
	        	
	        	ThematicMap thematicMap =(ThematicMap)thematicMapDao.loadXml( layerElt.getChild( "TMapThematicMap" ) );
	        	thematicMap.setFile( new File( name ) );
	        	
	            ThematicMapLayer layer =(ThematicMapLayer)bof.get( BizObjects.THEMATIC_MAP_LAYER );
	            layer.setThematicMap( thematicMap );
	            layer.setVisible( visible );
	            layer.setBaseMapVisible( baseMapVisible );
	            layer.setCdVisible( cdVisible );
	            layer.setAnnotationsVisible( annotationsVisible );
	            layer.setName( name );
	            layer.setFile( new File( file ) );
	            
	        	mltm.getLayers().add( layer );
	        }
        } catch( Exception e ) {
        	throw new TMapDAOException( e );
        }

        return mltm;
     }
    
	public BizObject load(String fileName) throws TMapDAOException {
		MultiLayerThematicMap mltm =null;

        try {
            Element rootElt =getRootElement( fileName );
            mltm =(MultiLayerThematicMap)loadXml( rootElt );
            mltm.setFile( new File( fileName ) );
        } catch( Exception e ) {
            throw new TMapDAOException( e );
        }
        
        return mltm;
	}

	public Element saveXml(BizObject object) throws TMapDAOException {
		MultiLayerThematicMap mltm =(MultiLayerThematicMap)object;
		
        Element rootElt =new Element( "TMapMultiLayerThematicMap" );
        rootElt.setAttribute( "version", VERSION );
        
        for( int ct =0; ct < mltm.getLayers().size(); ct++ ) {
        	ThematicMapLayer layer =mltm.getLayers().get( ct );
        	ThematicMapDAO thematicMapDao =(ThematicMapDAO)daof.get( BizObjects.THEMATIC_MAP );
        	
        	Element layerElt =new Element( "Layer" );
        	rootElt.addContent( layerElt );
        	
        	layerElt.setAttribute( "name", layer.getName() );
        	layerElt.setAttribute( "file", layer.getFile() != null ? layer.getFile().getAbsolutePath() : "" );
        	layerElt.setAttribute( "visible", String.valueOf( layer.isVisible() ) );
        	layerElt.setAttribute( "baseMapVisible", String.valueOf( layer.isBaseMapVisible() ) );
        	layerElt.setAttribute( "cartographicDisplayVisible", String.valueOf( layer.isCdVisible() ) ); 
        	layerElt.setAttribute( "annotationsVisible", String.valueOf( layer.isAnnotationsVisible() ) );          	
        	
        	Element tmElt =thematicMapDao.saveXml( layer.getThematicMap() );
        	
        	layerElt.addContent( tmElt );
    	}
                
		return rootElt;
	}

}
